package com.dong.vdbtc.typeconversion.conversion;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * description
 *
 * @author Administrator 2021/09/08 14:53
 */
@Component
public class MyService {

    private ConversionService conversionService;

    public ConversionService getConversionService() {
        return conversionService;
    }

    @Autowired
    public void setConversionService(ConversionService conversionService) {
        this.conversionService = conversionService;
    }

    public void doConversion(){
        //转换基本类型
        Integer result = this.conversionService.convert("23",Integer.class);
        System.out.println(result);
        //转换复杂类型
        List<Integer> list = new ArrayList<>();
        list.add(23);
        list.add(456);
        List<String> results = (List<String>) this.conversionService.convert(list, TypeDescriptor.forObject(list),TypeDescriptor.collection(List.class,TypeDescriptor.valueOf(String.class)));
        System.out.println(results);
    }
}
